package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.Keyword;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.KeywordEditorialStatus;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.KeywordStatus;
import com.genexususa.eo.bing.CampaignManagementServiceClasses.MatchType;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GxKeyword extends Keyword {
    private long gxId;

    public GxKeyword(Element element) throws IllegalArgumentException, GxItemException {
        if (element.getElementsByTagName("BroadMatchBid").getLength() > 0 ) {
            this.setBid(new GxBid((Element)element.getElementsByTagName("BroadMatchBid").item(0)));
            this.setMatchType(MatchType.Broad);
        }
        if (element.getElementsByTagName("ContentMatchBid").getLength() > 0) {
            this.setBid(new GxBid((Element)element.getElementsByTagName("ContentMatchBid").item(0)));
            this.setMatchType(MatchType.Content);
        }
        if (element.getElementsByTagName("EditorialStatus").getLength() > 0 &&
            element.getElementsByTagName("EditorialStatus").item(0).getChildNodes().getLength() > 0) {
            this.setEditorialStatus(KeywordEditorialStatus.fromString(((Node)element.getElementsByTagName("EditorialStatus").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        if (element.getElementsByTagName("ExactMatchBid").getLength() > 0) {
            this.setBid(new GxBid((Element) element.getElementsByTagName("ExactMatchBid").item(0)));
            this.setMatchType(MatchType.Exact);
        }
        if (element.getElementsByTagName("GxKeywordId").getLength() > 0 &&
            element.getElementsByTagName("GxKeywordId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxKeywordId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxId = Long.valueOf(((Node)element.getElementsByTagName("GxKeywordId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external Keyword id");
        }
        if (element.getElementsByTagName("BingKeywordId").getLength() > 0 &&
            element.getElementsByTagName("BingKeywordId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingKeywordId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setId(Long.valueOf(((Node) element.getElementsByTagName("BingKeywordId").item(0).getChildNodes().item(0)).getNodeValue()));
        }

        /*
        NodeList negativeKeywordsNode = element.getElementsByTagName("NegativeKeywordCollection");
        if (negativeKeywordsNode.getLength() > 0) {
            NodeList negativeKeywordNode = ((Element)negativeKeywordsNode.item(0)).getElementsByTagName("NegativeKeyword");
            String[] negativeKeywords = new String[negativeKeywordNode.getLength()];
            for (int i=0; i<negativeKeywordNode.getLength(); i++) {
                if (negativeKeywordNode.item(i).getChildNodes().getLength() > 0) {
                    negativeKeywords[i] = negativeKeywordNode.item(i).getChildNodes().item(0).getNodeValue();
                }
            }
            this.setNegativeKeywords(negativeKeywords);

        }
        */

        if (element.getElementsByTagName("Param1").getLength() > 0 &&
            element.getElementsByTagName("Param1").item(0).getChildNodes().getLength() > 0) {
            this.setParam1(((Node)element.getElementsByTagName("Param1").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Param2").getLength() > 0 &&
            element.getElementsByTagName("Param2").item(0).getChildNodes().getLength() > 0) {
            this.setParam2(((Node)element.getElementsByTagName("Param2").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("Param3").getLength() > 0 &&
            element.getElementsByTagName("Param3").item(0).getChildNodes().getLength() > 0) {
            this.setParam3(((Node)element.getElementsByTagName("Param3").item(0).getChildNodes().item(0)).getNodeValue());
        }
        if (element.getElementsByTagName("PhraseMatchBid").getLength() > 0) {
            this.setBid(new GxBid((Element)element.getElementsByTagName("PhraseMatchBid").item(0)));
            this.setMatchType(MatchType.Phrase);
        }
        if (element.getElementsByTagName("Status").getLength() > 0 &&
            element.getElementsByTagName("Status").item(0).getChildNodes().getLength() > 0) {
            if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("A")) {
                this.setStatus(KeywordStatus.Active);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("D")) {
                this.setStatus(KeywordStatus.Deleted);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("I")) {
                this.setStatus(KeywordStatus.Inactive);
            } else if (((Node) element.getElementsByTagName("Status").item(0).getChildNodes().item(0)).getNodeValue().equals("P")) {
                this.setStatus(KeywordStatus.Paused);
            }
        }
        if (element.getElementsByTagName("Text").getLength() > 0 &&
            element.getElementsByTagName("Text").item(0).getChildNodes().getLength() > 0) {
            this.setText(((Node)element.getElementsByTagName("Text").item(0).getChildNodes().item(0)).getNodeValue());
        }

        /*if (element.getElementsByTagName("MatchType").getLength() > 0 &&
                element.getElementsByTagName("MatchType").item(0).getChildNodes().getLength() > 0) {
            String matchType = ((Node)element.getElementsByTagName("MatchType").item(0).getChildNodes().item(0)).getNodeValue();

            if (matchType.equals("E")){
                this.setMatchType(MatchType.Exact);
            }
            if (matchType.equals("P")){
                this.setMatchType(MatchType.Phrase);
            }
            if (matchType.equals("C")){
                this.setMatchType(MatchType.Content);
            }
            if (matchType.equals("B")){
                this.setMatchType(MatchType.Broad);
            }
        }*/

        if (element.getElementsByTagName("Bid").getLength() > 0 &&
            element.getElementsByTagName("Bid").item(0).getChildNodes().getLength() > 0) {
            this.setBid(new GxBid((Element)element.getElementsByTagName("Bid").item(0)));
        }

        if (element.getElementsByTagName("DestinationUrl").getLength() > 0 &&
            element.getElementsByTagName("DestinationUrl").item(0).getChildNodes().getLength() > 0) {
            this.setDestinationUrl(((Node)element.getElementsByTagName("DestinationUrl").item(0).getChildNodes().item(0)).getNodeValue());

        }
    }

    public long getGxId() {
        return gxId;
    }

    public void setGxId(long gxId) {
        this.gxId = gxId;
    }
}
